﻿#-------------------------------------------------------------------------------
# Copyright 2011 the original author or authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#-------------------------------------------------------------------------------
#$Id: Python学习笔记（5）：函数.py 4 2011-03-22 15:48:45Z fangqing.fan@gmail.com $
#$Author: fangqing.fan@gmail.com $
#$Date: 2011-03-22 15:48:45 +0000 (Tue, 22 Mar 2011) $
#$Rev: 4 $
#$URL: http://fanfq-python-demo.googlecode.com/svn/trunk/helloworld/Python%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%EF%BC%885%EF%BC%89%EF%BC%9A%E5%87%BD%E6%95%B0.py $
#-------------------------------------------------------------------------------

#在前面我们已经见过一些Python内建函数，比如len和rang。现在我们来看看自定义函数，函数是通过def关键字来定义，后面跟函数名称和圆括号，括号内可以包含参数，该行以冒号结束，接下来是语句块，即函数体。
#1. 简单的sayHello函数
def sayHello():
    print("hello world")

#函数调用
sayHello()
#-------------------------------------------------------------------------------
#2. 带形参函数
def printMax(a,b):
    if a > b:
        print(a,"is max number")
    else:
        print(b,"is max number")

printMax(2,4)
#-------------------------------------------------------------------------------
#3. 局部变量
def func(x):
    print("x is",x)
    x = 2
    print("Changed local x to",x)

x = 50
func(x)
print("x is still",x)

#运行结果为：
#x is 50
#Changed local x to 2
#x is still 50
#-------------------------------------------------------------------------------
#4. 默认参数值
def say(message,times = 1):
    print(message * times)

say("ha")
say("ha",2)

#运行结果为：
#ha
#haha
#-------------------------------------------------------------------------------
#5. 关键参数
def func5(a,b = 5,c = 10):
    print("a is",a,"and b is",b,"and c is",c)

func5(3,7)
func5(25,c=4)
func5(c = 50,a=100)
#运行结果为：
#a is 3 and b is 7 and c is 10
#a is 25 and b is 5 and c is 4
#a is 100 and b is 5 and c is 50
#-------------------------------------------------------------------------------
#6. return语句
def maximum6(x,y):
    if x >y:
        return x
    else:
        return y

print(maximum6(2,3))
#-------------------------------------------------------------------------------
#7. 文档字符串DocStrings
#Python有一个奇妙的特性，就是文档字符串（DocStrings），它的主要作用就是帮助你的程序文档更加简单易懂。
def printMax7(x,y):
    "Prints the maximum of two numbers.The two values must be integers."
    x = int(x)
    y = int(y)
    if x > y:
        print(x,"is max number")
    else:
        print(y,"is max number")

printMax7(2,6)
print(printMax7.__doc__)
help(printMax7)

#6 is max number
#Prints the maximum of two numbers.The two values must be integers.
#Help on function printMax7 in module __main__:

#printMax7(x, y)
#    Prints the maximum of two numbers.The two values must be integers.
